package com.android.fitcontrol.common;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;

import com.android.fitcontrol.ExerciseListActivity;
import com.android.fitcontrol.LoginActivity;
import com.android.fitcontrol.MonthScheduleActivity;
import com.android.fitcontrol.bean.Training;
import com.android.fitcontrol.bean.User;
import com.android.fitcontrol.database.DBConnector;

public class CommonUtils {
	
	private static int user_id;
	/**
	 * Set Locale and reload activity.
	 * 
	 * @param localeName
	 *            - name locale "ru", "en".
	 */
	public static void setLocale(String localeName, Context context) {
		Locale locale = new Locale(localeName);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		context.getResources().updateConfiguration(config,
				context.getResources().getDisplayMetrics());
	}
	
	/**
	 * method for create preloginned user
	 * @param login
	 * @param password
	 */
	public static void createPreloginnedUser(String login, String password, DBConnector connector){
		User userSession = new User();
		userSession.set_ID(-1);
		userSession.setUserName(login);
		userSession.setPassword(password);
		connector.insert(userSession);
	}
	
	/**
	 * method for checking preloggined user
	 * and redirect to list activity if this user exists
	 */
	public static void checkPreloginnedUser(Activity activity,
			DBConnector connector) {
		User userSes = (User) connector.selectOneRowFromTable(User.TABLE_NAME,
				-1);
		Intent intent = new Intent();
		if (userSes == null) {
			intent.setClass(activity, LoginActivity.class);
			activity.startActivity(intent);
			activity.finish();
		} else {
			//getting user_id
			String where = User.USER_NAME + " = ? and " + User._ID + " > -1";
			String [] args = new String[] {userSes.getUserName()}; 
			Cursor c = connector.query(User.TABLE_NAME, null, where, args, null, null, null);
			c.moveToFirst();
			setUserID(c.getInt(0));
			c.close();
			//end setting user_id
			
			Intent exerciseListIntent = new Intent(activity, ExerciseListActivity.class);
			exerciseListIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Format formatter = new SimpleDateFormat("dd-MM-yyyy");
			Date resultDate = new Date();
			Calendar calendar = new GregorianCalendar();
			String dat = calendar.get(Calendar.DAY_OF_MONTH) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.YEAR);
			try{
				resultDate = (Date) formatter.parseObject(dat);
			}
			catch(Exception e){
				e.printStackTrace();
			}
			exerciseListIntent.putExtra("pickedDate", resultDate.getTime());
			String wheree = Training.USER_ID + " = ? and " + Training.DATE + " = ?";
			String [] argse = new String[]{String.valueOf(getUserId()), String.valueOf(resultDate.getTime())};
			Cursor cur = connector.query(Training.TABLE_NAME, null, wheree, argse, null, null, null);
			boolean isExistTraining = false;
			if(cur.moveToFirst()){
				isExistTraining = true;
				exerciseListIntent.putExtra("training_id", cur.getInt(Training.NUM_ID));
			}
			cur.close();
			if(isExistTraining){
				activity.startActivity(exerciseListIntent);
				activity.finish();
			}
			else{
				Intent calendarIntent = new Intent(activity, MonthScheduleActivity.class);
				calendarIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				calendarIntent.putExtra("isUp", false);
				activity.startActivity(calendarIntent);
				activity.finish();
			}
		}

	}
	public static int getUserId(){
		return user_id;
	}
	public static void setUserID(int id){
		user_id = id;
	}
}
